package com.example.ysm.widegettest_416ysm;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    //1
   private TextView tv;
    private EditText et;
    private Button btngettext,btnsetimg;
    private ImageView iv;
    private ImageButton ibtn;
//声明8个控件
    private RadioGroup rg;
    private RadioButton rbnan,rbnv;
    private CheckBox cblan,cbzu,cbyou;
    private Button btngetgender,btngetfun;

    private String strfun="";//初始化兴趣字符串
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init(){
        //2 获取六个控件
          tv=(TextView)findViewById(R.id.textView2);
        et=(EditText) findViewById(R.id.editText);
        btngettext=(Button) findViewById(R.id.button);
        btnsetimg=(Button) findViewById(R.id.button4);
       iv=(ImageView) findViewById(R.id.imageView);
        ibtn=(ImageButton) findViewById(R.id.imageButton);


      rg= (RadioGroup) findViewById(R.id.radg);
        rbnan= (RadioButton) findViewById(R.id.radioButton10);
        rbnv= (RadioButton) findViewById(R.id.radioButton9);
        cblan= (CheckBox) findViewById(R.id.checkBox3);
        cbzu= (CheckBox) findViewById(R.id.checkBox2);
        cbyou= (CheckBox) findViewById(R.id.checkBox);
        btngetgender= (Button) findViewById(R.id.button5);
        btngetfun= (Button) findViewById(R.id.button6);

        //设置按钮监听器
        btngettext.setOnClickListener(this);
        btnsetimg.setOnClickListener(this);
        ibtn.setOnClickListener(this);

        //按钮监听器
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case  R.id.radioButton9:
                        tv.setText("女");
                        break;
                    case  R.id.radioButton10:
                        tv.setText("男");
                        break;


                }
            }
        });
       cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
           if(isChecked)//篮球勾选
               strfun+="篮球+";
               else//篮球未被勾选
               strfun=strfun.replace("篮球+","");
               tv.setText(strfun);
           }
       });
        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)//游泳勾选
                    strfun+="游泳+";
                else//游泳未被勾选
                    strfun=strfun.replace("游泳+","");
                tv.setText(strfun);
            }
        });
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)//足球勾选
                    strfun+="足球+";
                else//足球未被勾选
                    strfun=strfun.replace("足球+","");
                tv.setText(strfun);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button://设置文本
                String str= et.getText().toString();
                tv.setText(str);
                break;
            case R.id.button4://设置图片
                iv.setImageResource(R.drawable.ysm);

                break;
            case R.id.imageButton://图片按钮
                     Intent intent =new Intent(MainActivity.this,NewActivity.class);
                //获取mainactivity里边的textview显示的东西
                intent.putExtra("param1",et.getText().toString());
                intent.putExtra("param2",3);
                startActivity(intent);

                break;

        }
    }
}
